package com.skp.shaphan.ui.dialogs;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.skp.shaphan.ShaphanApp;
import com.skp.shaphan.savedConnections.NewSavedConnection;
import com.skp.shaphan.ui.ActionBasedPanel;
import com.skp.shaphan.ui.buttons.ButtonsPane;
import com.skp.shaphan.ui.buttons.DialogCancelButton;
import com.skp.shaphan.ui.buttons.ConnectionOKButton;
import com.skp.shaphan.ui.connectioncontrols.ConnectionOptions;
import com.skp.shaphan.ui.connectioncontrols.DerbyConnectionOptions;
import com.skp.shaphan.ui.connectioncontrols.JTDSConnectionOptions;
import com.skp.shaphan.ui.connectioncontrols.MySQLConnectionOptions;
import com.skp.shaphan.ui.connectioncontrols.ODBCConnectionOptions;
import com.skp.shaphan.ui.connectioncontrols.OracleConnectionOptions;
import com.skp.shaphan.ui.connectioncontrols.SQLServerConnectionOptions;
import com.skp.shaphan.ui.connectioncontrols.SavedConnectionOptions;

/**
 * This dialog allows a user to connect to a database.
 * @author Stephen Phillips
 *
 */
public class ConnectionDialog extends JDialog implements ChangeListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2010714151920082219L;
	private ConnectionOKButton okButton;
	private JTabbedPane connectionInfo;
	private SavedConnectionOptions savedConnectionOptions;
	private DerbyConnectionOptions derbyOptions;
	private SQLServerConnectionOptions sqlServerOptions;
	private ODBCConnectionOptions odbcOptions;
	private OracleConnectionOptions oracleOptions;
	private MySQLConnectionOptions mySQLOptions;
	private JTDSConnectionOptions jtdsOptions;
	private NewSavedConnection newSavedConnection;
	private JTextField connectionName;
	
	public ConnectionDialog() {
		super(ShaphanApp.getMainWindow());
		
		this.setLayout(new GridBagLayout());
		int row = 0;

		this.add(new JLabel("Connection Name:"), new GridBagConstraints(0, row, 1, 1, 0.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 5));
		connectionName = new JTextField();
		this.add(connectionName, new GridBagConstraints(1, row++, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 5, 5));
		
		connectionInfo = new JTabbedPane();
		derbyOptions = new DerbyConnectionOptions();
		sqlServerOptions = new SQLServerConnectionOptions();
		odbcOptions = new ODBCConnectionOptions();
		oracleOptions = new OracleConnectionOptions();
		mySQLOptions = new MySQLConnectionOptions();
		jtdsOptions = new JTDSConnectionOptions();
		savedConnectionOptions = new SavedConnectionOptions(this);
		connectionInfo.addTab("Saved Connections", savedConnectionOptions);
		connectionInfo.addTab("Derby", derbyOptions);
		connectionInfo.addTab("SQL Server", sqlServerOptions);
		connectionInfo.addTab("ODBC", odbcOptions);
		connectionInfo.addTab("Oracle", oracleOptions);
		connectionInfo.addTab("MySQL", mySQLOptions);
		connectionInfo.addTab("jTDS", jtdsOptions);
		connectionInfo.addChangeListener(this);
		this.add(connectionInfo, new GridBagConstraints(0, row++, 2, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 5, 5));
		
		okButton = new ConnectionOKButton(this);
		newSavedConnection = new NewSavedConnection(this);
		this.add(new ButtonsPane(newSavedConnection, new DialogCancelButton(this), okButton), new GridBagConstraints(0, row++, 2, 1, 1.0, 1.0, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 5, 5));
		this.pack();

		this.setOKAction();
	}
	public void setOKAction(Runnable connectAction) {
		okButton.setOKAction(connectAction);
	}
	public void setOKAction() {
		Runnable action = ((ActionBasedPanel)connectionInfo.getSelectedComponent()).getAction();
		this.setOKAction(action);
		if(connectionInfo.getSelectedIndex() == 0) {
			newSavedConnection.setEnabled(false);
		} else {
			newSavedConnection.setEnabled(true);
		}
	}
	public Runnable getOKAction() {
		return okButton.getOKAction();
	}
	@Override
	public void stateChanged(ChangeEvent e) {
		this.setOKAction();
	}
	public void changeToTab(Class<? extends JPanel> tabClass) {
		for(int x=0; x<connectionInfo.getTabCount(); x++) {
			if(connectionInfo.getComponentAt(x).getClass().getName().equals(tabClass.getName())) {
				connectionInfo.setSelectedIndex(x);
				return;
			}
		}
	}
	public void changeToTab(String tabClassName) {
		for(int x=0; x<connectionInfo.getTabCount(); x++) {
			if(connectionInfo.getComponentAt(x).getClass().getName().equals(tabClassName)) {
				connectionInfo.setSelectedIndex(x);
				return;
			}
		}
	}
	public ConnectionOptions getTabForClass(String className) {
		for(int x=0; x<connectionInfo.getTabCount(); x++) {
			if(connectionInfo.getComponentAt(x).getClass().getName().equals(className)) {
				return (ConnectionOptions) connectionInfo.getComponentAt(x);
			}
		}
		return null;
	}
	public ConnectionOptions getCurrentConnectionOptions() {
		return (ConnectionOptions) connectionInfo.getComponentAt(connectionInfo.getSelectedIndex());
	}
	
	public void setConnectionName(String name) {
		connectionName.setText(name);
	}
	
	public String getConnectionName() {
		return connectionName.getText();
	}

}
